#include "includes/see_state.h"
#include "includes/game.h"
#include <stdio.h>
#include <math.h>

See_State_Env * ss_env(){
	return (See_State_Env *) game()->see_state.env;
}

void see_state_init(){
	skybox_init(&ss_env()->skybox);
	house_init(&ss_env()->house);
	
	see_state_init_gl(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
	ss_env()->angle = 0;
	glutPostRedisplay();
	glutTimerFunc(50, see_state_compute, 0);
}

void see_state_init_gl(int width, int height){
	
	/* Calcul du ratio de la fenetre pour 
	 * conserver les proportions 
	 */
	GLdouble ratio = (GLdouble)width / (GLdouble)height;
	
	glViewport(0, 0, width, height);
	
	/* Pour gerer les zIndex */
    glEnable(GL_DEPTH_TEST);
	
	 /* Mise en place de la projection en perspective */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(50.0, ratio, 1., 500.0);
	
	/* Matrice de manipulation des objets */
    glMatrixMode(GL_MODELVIEW);
}

void see_state_main(){
	see_state_draw();
}

void see_state_compute(){
	ss_env()->angle = fmod(ss_env()->angle + 0.02, 2 * M_PI);
	glutTimerFunc(50, see_state_compute, 0);
	glutPostRedisplay();
}

void see_state_draw(){
	
	float x, y;
	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    /* Changement de position de la camera */
	x = 30. * cos(ss_env()->angle);
    y = 30. * sin(ss_env()->angle);
	gluLookAt(x, y, 10., 0., 0., 1., 0, 0, 1);
	
	skybox_draw(&ss_env()->skybox);
	house_draw(&ss_env()->house);
	
	glutSwapBuffers();
}

void see_state_event_keyboard(unsigned char key, int x, int y){}

void see_state_event_special(int key, int x, int y){}

void see_state_resize(int width, int height){
	see_state_init_gl(width, height);
}
